package org.mobile.logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.mobile.beans.Phone;
import org.mobile.database.connection.ConnectionManager;
import org.mobile.database.statements.Statements;

public class PhoneLogic {
	
private static PhoneLogic instance;
	
	private PhoneLogic(){
		
	}
	
	public static PhoneLogic getInstance(){
		if(instance == null){
			instance= new PhoneLogic();
		}
		return instance;
	}
	
	public boolean addPhone(Phone phone, String HSDPAITD){
		
		Connection conn= null;
		PreparedStatement st= null;
		
		
		System.out.println(HSDPAITD);
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.ADD_PHONE);
			st.setString(1, phone.getModel());
			st.setInt(2, phone.getPrice());
			st.setFloat(3, phone.getHardiness());
			st.setFloat(4, phone.getCamera());
			st.setFloat(5, phone.getHeight());
			st.setFloat(6, phone.getWidth());
			st.setFloat(7, phone.getDepth());
			st.setFloat(8, phone.getWeight());
			st.setFloat(9, phone.getColors());
			st.setString(10, phone.getResolution());
			st.setFloat(11, phone.getScreenSize());
			st.setFloat(12, phone.getTalkTime2g());
			st.setFloat(13, phone.getTalkTime3g());
			st.setFloat(14, phone.getStandBy2g());
			st.setFloat(15, phone.getStandBy3g());
			st.setFloat(16, phone.getBuildInMem());
			st.setInt(17, phone.getRam());
			st.setFloat(18, phone.getMaxCardSize());
			st.setFloat(19, phone.getNrOfApp());
			st.setFloat(20, phone.getProcClock());
			st.setFloat(21, phone.getReleaseDate());
			st.setFloat(22, phone.getPopularity());
			st.setInt(23, Integer.parseInt(HSDPAITD));
			st.setInt(24, phone.getGorillaglass());
			st.setFloat(25, phone.getDesign());
			st.setInt(26, phone.getAutofocus());
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean updatePhone(Phone phone){
		Connection conn= null;
		PreparedStatement st= null;
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.UPDATE_PHONE);
			st.setString(1, phone.getModel());
			st.setInt(2, phone.getPrice());
			st.setFloat(3, phone.getHardiness());
			st.setFloat(4, phone.getCamera());
			st.setFloat(5, phone.getHeight());
			st.setFloat(6, phone.getWidth());
			st.setFloat(7, phone.getDepth());
			st.setFloat(8, phone.getWeight());
			st.setFloat(9, phone.getColors());
			st.setString(10, phone.getResolution());
			st.setFloat(11, phone.getScreenSize());
			st.setFloat(12, phone.getTalkTime2g());
			st.setFloat(13, phone.getTalkTime3g());
			st.setFloat(14, phone.getStandBy2g());
			st.setFloat(15, phone.getStandBy3g());
			st.setFloat(16, phone.getBuildInMem());
			st.setInt(17, phone.getRam());
			st.setFloat(18, phone.getMaxCardSize());
			st.setFloat(19, phone.getNrOfApp());
			st.setFloat(20, phone.getProcClock());
			st.setFloat(21, phone.getReleaseDate());
			st.setFloat(22, phone.getPopularity());
			st.setInt(23, phone.getHsdpaitd());
			st.setInt(24, phone.getGorillaglass());
			st.setFloat(25, phone.getDesign());
			st.setInt(26, phone.getAutofocus());
			st.setString(27, phone.getModel());
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public List<Phone> getPhones(int orderID){
		Connection conn= null;
		PreparedStatement st= null;
		ResultSet rs= null;
		ArrayList<Phone> phoneList= new ArrayList<Phone>();
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.SELECT_PHONE_BY_ORDER);
			st.setInt(1, orderID);
			rs= st.executeQuery();
			while(rs.next()){
				Phone phone= new Phone();
				phone.setPhoneId(rs.getInt(1));
				phone.setModel(rs.getString(2));
				phone.setPrice(rs.getInt(3));
				phone.setHardiness(rs.getFloat(4));
				phone.setCamera(rs.getFloat(5));
				phone.setHeight(rs.getFloat(6));
				phone.setWidth(rs.getFloat(7));
				phone.setDepth(rs.getFloat(8));
				phone.setWeight(rs.getFloat(9));
				phone.setColors(rs.getFloat(10));
				phone.setResolution(rs.getString(11));
				phone.setScreenSize(rs.getFloat(12));
				phone.setTalkTime2g(rs.getFloat(13));
				phone.setTalkTime3g(rs.getFloat(14));
				phone.setStandBy2g(rs.getFloat(15));
				phone.setStandBy3g(rs.getFloat(16));
				phone.setBuildInMem(rs.getFloat(17));
				phone.setRam(rs.getInt(18));
				phone.setMaxCardSize(rs.getFloat(19));
				phone.setNrOfApp(rs.getFloat(20));
				phone.setProcClock(rs.getFloat(21));
				phone.setReleaseDate(rs.getFloat(22));
				phone.setPopularity(rs.getFloat(23));
				phone.setHsdpaitd(rs.getInt(24));
				phone.setGorillaglass(rs.getInt(25));
				phone.setDesign(rs.getFloat(26));
				phone.setAutofocus(rs.getInt(27));
				phoneList.add(phone);
			}
			return phoneList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		finally{
			try {
				rs.close();
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean removePhone(int phoneID){
		Connection conn= null;
		PreparedStatement st= null;
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.REMOVE_PHONE);
			st.setInt(1, phoneID);
			st.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally{
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public List<Phone> getAllPhones(){
		Connection conn= null;
		Statement st= null;
		ResultSet rs= null;
		ArrayList<Phone> phoneList= new ArrayList<Phone>();
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.createStatement();
			System.out.println("st = " + st);
			rs= st.executeQuery(Statements.SELECT_ALL_PHONES);
			while(rs.next()){
				Phone phone= new Phone();
				phone.setPhoneId(rs.getInt(1));
				phone.setModel(rs.getString(2));
				phone.setPrice(rs.getInt(3));
				phone.setHardiness(rs.getFloat(4));
				phone.setCamera(rs.getFloat(5));
				phone.setHeight(rs.getFloat(6));
				phone.setWidth(rs.getFloat(7));
				phone.setDepth(rs.getFloat(8));
				phone.setWeight(rs.getFloat(9));
				phone.setColors(rs.getFloat(10));
				phone.setResolution(rs.getString(11));
				phone.setScreenSize(rs.getFloat(12));
				phone.setTalkTime2g(rs.getFloat(13));
				phone.setTalkTime3g(rs.getFloat(14));
				phone.setStandBy2g(rs.getFloat(15));
				phone.setStandBy3g(rs.getFloat(16));
				phone.setBuildInMem(rs.getFloat(17));
				phone.setRam(rs.getInt(18));
				phone.setMaxCardSize(rs.getFloat(19));
				phone.setNrOfApp(rs.getFloat(20));
				phone.setProcClock(rs.getFloat(21));
				phone.setReleaseDate(rs.getFloat(22));
				phone.setPopularity(rs.getFloat(23));
				phone.setHsdpaitd(rs.getInt(24));
				phone.setGorillaglass(rs.getInt(25));
				phone.setDesign(rs.getFloat(26));
				phone.setAutofocus(rs.getInt(27));
				phoneList.add(phone);
			}
			return phoneList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		finally{
			try {
				rs.close();
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public List<Phone> searchPhone(Phone phone){
		Connection conn= null;
		PreparedStatement st= null;
		ResultSet rs= null;
		ArrayList<Phone> phoneList= new ArrayList<Phone>();
		
		conn= ConnectionManager.getManager().getConnection();
		try {
			st= conn.prepareStatement(Statements.SEARCH_PHONE);
			st.setInt(1, phone.getPrice());
			st.setFloat(2, phone.getScreenSize());
			st.setInt(3, phone.getRam());
			st.setFloat(4, phone.getProcClock());
			st.setFloat(5, phone.getNrOfApp());
			st.setInt(6, phone.getAutofocus());
			st.setInt(7, phone.getGorillaglass());
			rs= st.executeQuery();
			
			while(rs.next()){
				Phone newPhone= new Phone();
				newPhone.setPhoneId(rs.getInt(1));
				newPhone.setModel(rs.getString(2));
				newPhone.setPrice(rs.getInt(3));
				newPhone.setHardiness(rs.getFloat(4));
				newPhone.setCamera(rs.getFloat(5));
				newPhone.setHeight(rs.getFloat(6));
				newPhone.setWidth(rs.getFloat(7));
				newPhone.setDepth(rs.getFloat(8));
				newPhone.setWeight(rs.getFloat(9));
				newPhone.setColors(rs.getFloat(10));
				newPhone.setResolution(rs.getString(11));
				newPhone.setScreenSize(rs.getFloat(12));
				newPhone.setTalkTime2g(rs.getFloat(13));
				newPhone.setTalkTime3g(rs.getFloat(14));
				newPhone.setStandBy2g(rs.getFloat(15));
				newPhone.setStandBy3g(rs.getFloat(16));
				newPhone.setBuildInMem(rs.getFloat(17));
				newPhone.setRam(rs.getInt(18));
				newPhone.setMaxCardSize(rs.getFloat(19));
				newPhone.setNrOfApp(rs.getFloat(20));
				newPhone.setProcClock(rs.getFloat(21));
				newPhone.setReleaseDate(rs.getFloat(22));
				newPhone.setPopularity(rs.getFloat(23));
				newPhone.setHsdpaitd(rs.getInt(24));
				newPhone.setGorillaglass(rs.getInt(25));
				newPhone.setDesign(rs.getFloat(26));
				newPhone.setAutofocus(rs.getInt(27));
				phoneList.add(newPhone);
			}
			return phoneList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		finally{
			try {
				rs.close();
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
